source('code/analysis/load.R')
defund = read_csv('data/working/defund-receiver-wordcounts.dta') %>%
  mutate(exp='defund')
deport = read_csv('data/working/deport-receiver-wordcounts.dta') %>%
  mutate(exp='deport')

get_chi2 = function(counts) {
  
  calculate_chi2 = function(count1, count2) {
    ncount2 = (total_noexcuse - count2)
    ncount1 = (total_excuse - count1)
    num = (count1*ncount2 - count2*ncount1)^2
    denom = (count1 + count2) * (count1 + ncount1) * (count2 + ncount2) * (ncount1 + ncount2)
    return (num / denom)
  }
  
  
  wide = counts %>% 
    filter(!is.na(word)) %>% 
    pivot_wider(names_from = condition,
                values_from = count) %>%
    mutate_if(is.numeric , replace_na, replace = 0)
  
  total_noexcuse = sum(wide$noexcuse)
  total_excuse = sum(wide$excuse)

  wide = wide %>% 
    mutate(total = excuse + noexcuse) %>% 
    rowwise() %>% 
    mutate(abschi2 = calculate_chi2(excuse, noexcuse),
           #pvalue = pbinom(min, total, 0.5),
           characteristic = ifelse(excuse>noexcuse, 'Cover', 'No Cover'),
           characteristic = factor(characteristic, levels = c('No Cover','Cover')),
           chi2 = ifelse(characteristic=='No Cover', -1*abschi2, abschi2)) 
  
  return(wide)
}


## Exp.2 ##
defund_chi_low = get_chi2(defund) %>%
  arrange(chi2) %>%
  mutate(chi2 = formatC(chi2, digits = 4, format = "f"))

defund_chi_high = get_chi2(defund) %>%
  arrange(desc(chi2)) %>%
  mutate(chi2 = formatC(chi2, digits = 4, format = "f"))

out = c('\\begin{table}[H]',
        '\\centering', 
        '\\caption{Experiment 2: most characteristic words of each condition}',
        str_interp('\\label{t:exp2-chi2}'),
        '\\vspace*{2mm}',
        '\\begin{tabular}{@{}lllll@{}}',
        '\\toprule',
        '\\textbf{Most characteristic of \\textit{Cover}} & \\textbf{Most characteristic of \\textit{No Cover}} \\\\',
        '\\midrule',
        str_interp('${defund_chi_high$word[1]} & ${defund_chi_low$word[1]} \\\\'),
        str_interp('${defund_chi_high$word[2]} & ${defund_chi_low$word[2]} \\\\'),
        str_interp('${defund_chi_high$word[3]} & ${defund_chi_low$word[3]} \\\\'),
        str_interp('${defund_chi_high$word[4]} & ${defund_chi_low$word[4]} \\\\'),
        str_interp('${defund_chi_high$word[5]} & ${defund_chi_low$word[5]} \\\\'),
        str_interp('${defund_chi_high$word[6]} & ${defund_chi_low$word[6]} \\\\'),
        str_interp('${defund_chi_high$word[7]} & ${defund_chi_low$word[7]} \\\\'),
        str_interp('${defund_chi_high$word[8]} & ${defund_chi_low$word[8]} \\\\'),
        str_interp('${defund_chi_high$word[9]} & ${defund_chi_low$word[9]} \\\\'),
        str_interp('${defund_chi_high$word[10]} & ${defund_chi_low$word[10]} \\\\'),
        '\\bottomrule',
        '\\end{tabular}',
        '\\begin{tablenotes} \\footnotesize',
        '\\item \\textit{Notes:} ',
        tablenotes['exp2-chi2'],
        '\\end{tablenotes}',
        '\\end{table}')
writeLines(out, "output/tables/defund-chi2.tex")


## Exp.4 ##
deport_chi_low = get_chi2(deport) %>%
  arrange(chi2) %>%
  mutate(chi2 = formatC(chi2, digits = 4, format = "f"))

deport_chi_high = get_chi2(deport) %>%
  arrange(desc(chi2)) %>%
  mutate(chi2 = formatC(chi2, digits = 4, format = "f"))

out = c('\\begin{table}[H]',
        '\\centering', 
        '\\caption{Experiment 4: most characteristic words of each condition}',
        str_interp('\\label{t:exp4-chi2}'),
        '\\vspace*{2mm}',
        '\\begin{tabular}{@{}lllll@{}}',
        '\\toprule',
        '\\textbf{Most characteristic of \\textit{Cover}} & \\textbf{Most characteristic of \\textit{No Cover}} \\\\',
        '\\midrule',
        str_interp('${deport_chi_high$word[1]} & ${deport_chi_low$word[1]} \\\\'),
        str_interp('${deport_chi_high$word[2]} & ${deport_chi_low$word[2]} \\\\'),
        str_interp('${deport_chi_high$word[3]} & ${deport_chi_low$word[3]} \\\\'),
        str_interp('${deport_chi_high$word[4]} & ${deport_chi_low$word[4]} \\\\'),
        str_interp('${deport_chi_high$word[5]} & ${deport_chi_low$word[5]} \\\\'),
        str_interp('${deport_chi_high$word[6]} & ${deport_chi_low$word[6]} \\\\'),
        str_interp('${deport_chi_high$word[7]} & ${deport_chi_low$word[7]} \\\\'),
        str_interp('${deport_chi_high$word[8]} & ${deport_chi_low$word[8]} \\\\'),
        str_interp('${deport_chi_high$word[9]} & ${deport_chi_low$word[9]} \\\\'),
        str_interp('${deport_chi_high$word[10]} & ${deport_chi_low$word[10]} \\\\'),
        '\\bottomrule',
        '\\end{tabular}',
        '\\begin{tablenotes} \\footnotesize',
        '\\item \\textit{Notes:} ',
        tablenotes['exp4-chi2'],
        '\\end{tablenotes}',
        '\\end{table}')
writeLines(out, "output/tables/deport-chi2.tex")

